package com.lionel.generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**
 * @description:
 * @author: liucan@yzw.cn
 * @time: 2022/3/17 12:24
 */
public class GeneratorService {

    @Test
    public void generateCode() {
        String packageName = "com.yzw.mf.workbench.core";
        //user -> UserService, 设置成true: user -> IUserService
        boolean serviceNameStartWithI = false;
        generateByTables(serviceNameStartWithI, packageName, "common_service"
                , "user_common_service"
                , "process_guide", "todo_list"
                , "user_todo_list"
                , "todo_item"
                , "business_advice"
                , "business_advice_tag");
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://172.16.0.157:5305/yzw_mf_center";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("app_owner")
                .setPassword("YZ.zk.owner@2016")
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                // 【实体】是否为lombok模型（默认 false)
                .setEntityLombokModel(true)
                .setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                //修改替换成你需要的表名，多个表名传数组
                .setInclude(tableNames)
                .setSuperEntityClass("BaseEntity")
                .setSuperEntityColumns("create_user_name", "create_user_id", "create_time",
                        "update_user_name", "update_user_id", "update_time","common_status");
        config.setActiveRecord(false)
                .setAuthor("liucan@yzw.cn")
                .setOutputDir(System.getProperty("user.dir") + "/src/main/java")
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setEntity("model")
                ).execute();
    }

    private void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }
}